package com.itzol.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itzol.config.Result;
import com.itzol.entity.Consume;
import com.itzol.entity.Reduce;
import com.itzol.entity.Users;
import com.itzol.mapper.ConsumeMapper;
import com.itzol.service.ConsumeService;
import com.itzol.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@RestController
public class ConsumeController {
    @Autowired
    private ConsumeService consumeService;
    @PostMapping("/consume")
    public Result consume(@RequestBody Consume consume){
        return consumeService.consume(consume);
    }
    //根据前端传来的手机号 查询出用户 然后对比用户的余额 和结款的money 最后更新consume表
    @Autowired
    private UsersService usersService;
    @PostMapping("/reduce")
    public Result reduce(@RequestBody Reduce reduce){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("Phone",reduce.getPhone());
        Users users =  usersService.getOne(wrapper);
        if(users.getMoney()>reduce.getMoney()){
               users.setMoney(users.getMoney()-reduce.getMoney());
               usersService.update(users,wrapper);
               Consume consume = new Consume();
               consume.setMoney(reduce.getMoney());
               consume.setConsumer(users.getId());
               consumeService.save(consume);
               return Result.succ("结账成功");
        }
        else {
            return Result.erreo("余额不足");
        }
    }
    @PostMapping("/query")
    public Result query(@RequestBody Reduce reduce){
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("Phone",reduce.getPhone());
        Users users =  usersService.getOne(wrapper);
        return Result.succ(users);
    }
    @Autowired
    private ConsumeMapper consumeMapper;

    @PostMapping("/getday")
    public Result getday(){
       return Result.succ(consumeMapper.Getday());
    }

    @PostMapping("/getmonth")
    public Result getmonth(){
        return Result.succ(consumeMapper.Getmonth());
    }
}
